//
//  YTEmbodyPasswordViewController.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/5/25.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTEmbodyPasswordViewController.h"
#import "YTUserProfileServices.h"
#import "YTSecureTextField.h"
#import <CocoaSecurity/CocoaSecurity.h>

@interface YTEmbodyPasswordViewController ()
@property (weak, nonatomic) IBOutlet UILabel *tipsText;
@property (weak, nonatomic) IBOutlet YTSecureTextField *textField;
/** 记录老密码*/
@property (copy, nonatomic) NSString *oldPassword;
@property (copy, nonatomic) NSString *password;
@property (assign, nonatomic) NSInteger step;
@end

@implementation YTEmbodyPasswordViewController

- (void)initialize {
    if (_type == 0) {
        self.navigationItem.title = @"重置提现密码";
        self.tipsText.text = @"输入原提现密码";
    } else {
        self.navigationItem.title = @"设置提现密码";
        self.tipsText.text = @"该6位数字密码将用于\n账户体现";
    }
}

- (IBAction)inputCompleteAction:(YTSecureTextField *)sender {
    [UIView transitionWithView:sender duration:0.3
                       options:UIViewAnimationOptionTransitionCurlUp
                    animations:nil completion:nil];
    
    [UIView transitionWithView:self.tipsText duration:0.5
                       options:UIViewAnimationOptionTransitionFlipFromLeft
                    animations:nil completion:nil];
    _step++;
    [self textChange];
    if (_type == YTEmbodyPasswordResetType) {
        if (_step == 1) {
            _oldPassword = self.textField.text;
        }
        
        if (_step == 2) {
            _password = self.textField.text;
        }
    }
    
    if (_type == YTEmbodyPasswordSetType && _step == 1) {
        _password = self.textField.text;
    }
    
    [sender cleanText];
}

- (void)textChange {
    if (_type == YTEmbodyPasswordSetType) {
        switch (_step) {
                case 0:
                self.tipsText.text = @"该6位数字密码将用于\n账户体现";
                break;
                case 1:
                case 2:
                self.tipsText.text = @"请再次输入以确认";
                break;
        }
        
        if (_step == 2) [self forgetAction];
    } else {
        switch (_step) {
                case 0:
                self.tipsText.text = @"输入原提现密码";
                break;
                case 1:
                self.tipsText.text = @"新的提现密码";
                break;
                case 2:
                self.tipsText.text = @"请再次输入以确认";
                break;
        }
        
        if (_step == 3) [self rememberAction];
    }
}

// 记得原密码
- (void)rememberAction {
    [SVProgressHUD show];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    NSString *oldPassword = [self passwordMD5:self.oldPassword];
    NSString *nowPassword = [self passwordMD5:self.password];
    dict[@"oldPassword"] = [CocoaSecurity md5:oldPassword].hex;
    dict[@"newPassword"] = [CocoaSecurity md5:nowPassword].hex;
    [[YTUserProfileServices rememberResetPayPassword:dict] subscribeNext:^(id x) {
        [SVProgressHUD showSuccessWithStatus:@"重置成功"];
        [self.navigationController popToRootViewControllerAnimated:YES];
    } error:^(NSError *error) {
        if (error) {
            _step = 0;
            _tipsText.text = @"输入原提现密码";
        }
    }];
}

// 忘记原密码,流程跟没有设置一样
- (void)forgetAction {
    [SVProgressHUD show];
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[@"mobileCode"] = _verifyCode;
    NSString *md5 = [self passwordMD5:self.password];
    NSString *password = [CocoaSecurity md5:md5].hex;
    dict[@"newPassword"] = password;
    [[YTUserProfileServices forgetResetPayPassword:dict] subscribeNext:^(id x) {
        [SVProgressHUD showSuccessWithStatus:@"设置成功"];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
}

- (NSString *)passwordMD5:(NSString *)password {
    return [NSString stringWithFormat:@"%@%@paypassword", password, [YTUtilityManager manager].userModel.userId];
}

@end
